package com.starlight.flower_server.controller.authentication;

import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.flowerUser.cqrs.FlowerUserLoginCommand;
import com.starlight.flower_server.domain.flowerUser.data.TokenData;
import com.starlight.flower_server.domain.service.FlowerUserService;
import com.starlight.flower_server.domain.service.LoginInfoService;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.FlowerUser;
import com.starlight.flower_server.infrastructure.entity.LoginInfo;
import com.starlight.flower_server.infrastructure.entity.Role;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import com.starlight.flower_server.infrastructure.util.JwtUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author yfy2001
 * @date 2023/5/16 16:56
 */
@RestController
@RequestMapping("/auth")
public class AuthenticationController {

    @Resource
    private FlowerUserService flowerUserService;

    @Resource
    private LoginInfoService loginInfoService;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Resource
    private JwtUtils jwtUtils;

    /**
     * 用户登录
     * @param command
     * @return
     * 从请求头获取ip和归属地
     * 自动保存一条登录记录
     */
    @PostMapping("/flowerUserLogin")
    public FlowerServerResponse<String> flowerUserLogin(@RequestBody FlowerUserLoginCommand command){
        FlowerUser flowerUser=new FlowerUser();
        if (command.getUsername()!=null){
            flowerUser.setUsername(command.getUsername());
        }
        if (command.getEmail()!=null){
            flowerUser.setEmail(command.getEmail());
        }
        flowerUser.setPassword(command.getPassword());
        TokenData tokenData=flowerUserService.login(flowerUser);
        LoginInfo loginInfo=new LoginInfo();
        loginInfo.setFlowerUserId(tokenData.getFlowerUserId());
        loginInfo.setIp(CurrentUserInfoUtils.get("ip"));
        loginInfo.setAddress(CurrentUserInfoUtils.get("address"));
        loginInfo.setDevice(CurrentUserInfoUtils.get("device"));
        loginInfoService.insertLoginInfo(loginInfo);
        return FlowerServerResponse.success(tokenData.getToken());
    }

    @PostMapping("/flowerUserLogout")
    public FlowerServerResponse<String> flowerUserLogout(HttpServletRequest request){
        String token=request.getHeader("token");
        return FlowerServerResponse.success(flowerUserService.logout(token));
    }

    /**
     * 登录验证
     * @param request
     * @return
     */
    @PostMapping("/loginVerify")
    public FlowerServerResponse<Boolean> loginVerify(HttpServletRequest request){
        String token=request.getHeader("token");
        if(token==null||(!jwtUtils.verifyToken(token))){
            throw new BadRequestException(Status.LOGIN_REQUIRED.getMessage(),Status.LOGIN_REQUIRED);
        }
        return FlowerServerResponse.success(true);
    }

    /**
     * 管理员认证
     * @param request
     * @return
     */
    @PostMapping("/roleVerify")
    public FlowerServerResponse<Boolean> roleVerify(HttpServletRequest request){
        String token=request.getHeader("token");
        String userRole = redisTemplate.opsForValue().get(token);
        if (userRole!=null){
            return FlowerServerResponse.success(userRole.equals(RoleType.ROOT.toString()));
        }else {
            return FlowerServerResponse.success(false);
        }
    }
}
